/*!
 * \file frhash2_free2.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static void my_list_node_free(Frlist_node* node)
{
    frfree(node->data);
}

static void __case1(void)
{
    Frhash_header *p_hash_hdr = NULL;

    frhash2_free2(p_hash_hdr, my_list_node_free);

    return;
}

static void __case2(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node = NULL;
    char *p_data = NULL;
    char *p_data1 = NULL;
    char *p_data2 = NULL;
    unsigned int ul_hash_size = 5;
    unsigned long ul_key = 0;

    p_hash_hdr = frhash2_create2(ul_hash_size);

    p_data = (char *)frmalloc(strlen("test") + 1);
    p_data1 = p_data;
    snprintf(p_data,strlen("test") + 1,"%s","test");
    ul_key = frhash_key((unsigned char *)p_data,strlen(p_data));

    p_st_node = frlist_node_create(p_data);
    frhash2_addLong(p_hash_hdr,ul_key,p_st_node);

    p_data = (char *)frmalloc(strlen("case") + 1);
    p_data2 = p_data;
    snprintf(p_data,strlen("case") + 1,"%s","case");
    ul_key = frhash_key((unsigned char *)p_data,strlen(p_data));

    p_st_node = frlist_node_create(p_data);
    frhash2_addLong(p_hash_hdr,ul_key,p_st_node);


    frhash2_free2(p_hash_hdr, NULL);
    frfree(p_data1);
    frfree(p_data2);

    return;
}

static void __case3(void)
{
    Frhash_header *p_hash_hdr = NULL;
    Frlist_node *p_st_node = NULL;
    char *p_data = NULL;
    unsigned int ul_hash_size = 5;
    unsigned long ul_key = 0;

    p_hash_hdr = frhash2_create2(ul_hash_size);

    p_data = (char *)frmalloc(strlen("test") + 1);
    snprintf(p_data,strlen("test") + 1,"%s","test");
    ul_key = frhash_key((unsigned char *)p_data,strlen(p_data));

    p_st_node = frlist_node_create(p_data);
    frhash2_addLong(p_hash_hdr,ul_key,p_st_node);

    p_data = (char *)frmalloc(strlen("case") + 1);
    snprintf(p_data,strlen("case") + 1,"%s","case");
    ul_key = frhash_key((unsigned char *)p_data,strlen(p_data));

    p_st_node = frlist_node_create(p_data);
    frhash2_addLong(p_hash_hdr,ul_key,p_st_node);


    frhash2_free2(p_hash_hdr, my_list_node_free);

    return;
}


int add_suite_frhash2_free2(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frhash2 frhash2_free2()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);

    return FR_SUCC;
}
